package com.example.work_sqlite_note;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Describe extends AppCompatActivity {
    private static final String TAG = "Describe";
    private Button sumit;
    private Button back;
    private EditText title;
    private EditText content;
    private TextView textView;
    private Boolean up_or_in = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_describe);
        back = findViewById(R.id.back);
        sumit = findViewById(R.id.submit);
        title = findViewById(R.id.title_edit);
        content = findViewById(R.id.content_edit);
        textView = findViewById( R.id.textView);
        des();
        sumit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sumit.getText().equals("修改")) {
                    content.setEnabled(true);
                    sumit.setText("保存");
                }else if (sumit.getText().equals("保存")){
                    String t = title.getText().toString();
                    String c1 = content.getText().toString();
                    Temp temp = new Temp();
                    temp.setContent(c1);
                    temp.updateAll("title=?",t);
                    reward();
                }
                else {
                    String t = title.getText().toString();
                    String c = content.getText().toString();
                    Temp temp = new Temp();
                    temp.setTitle(t);
                    temp.setContent(c);
                    temp.save();
                    reward();
                }
            }
        });


        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void reward(){
        up_or_in = false;
        Intent intent = new Intent();
        intent.putExtra("returning", "returning");
        setResult(RESULT_OK, intent);
        Log.d(TAG, "onClick: 返回上一个");
        finish();

    }

    private void des() {
        Intent intent = getIntent();
        if (intent == null) {
            return;
        }
        String l = intent.getStringExtra("list");
        Log.d(TAG, "des: l"+l);
        if ("list".equals(l)){
            textView.setText("详情信息");
            title.setEnabled(false);
            content.setEnabled(false);

            sumit.setText("修改");
        }
        else
        {
            textView.setText("添加事项");

        }
        String title_values = intent.getStringExtra("title");
        String content_values = intent.getStringExtra("content");
        Log.d(TAG, "des: " + title_values);
        Log.d(TAG, "des: " + content_values);


        title.setText(title_values);
        content.setText(content_values);
        up_or_in = true;


    }

    @Override
    public void finish() {
        super.finish();
    }
}
